__author__ = """waen <wangjian@21epub.com>"""
__docformat__ = 'restructuredtext'


from AccessControl import ClassSecurityInfo
from zope.interface import implements

from Products.ATContentTypes.content.base import registerATCT

from Products.Archetypes import atapi
from Products.ATContentTypes.content.folder import ATFolderSchema
from Products.ATContentTypes.configuration import zconf

from Products.CMFCore.utils import getToolByName

from Products.ePub.config import PROJECTNAME
from Products.ePub.interfaces import IComixSite
from Products.ePub.content.folder import ePubFolder
from Products.ePub import ePubMessageFactory as _

# referenceDataGrid
#from collective.referencedatagridfield import ReferenceDataGridField
#from collective.referencedatagridfield import ReferenceDataGridWidget

#  relatedItems 
from Products.CMFCore.permissions import ModifyPortalContent
from archetypes.referencebrowserwidget import ReferenceBrowserWidget

#image relation
from Products.validation.config import validation
from Products.validation.validators.SupplValidators import MaxSizeValidator
from Products.validation import V_REQUIRED

validation.register(MaxSizeValidator('checkImageMaxSize',
                                     maxsize=zconf.ATImage.max_file_size))

# Schema definition
ComixSiteSchema = ATFolderSchema.copy() + atapi.Schema((
                                                        
                                                        
    atapi.ReferenceField('relatedItems',
                          relationship = 'relatesTo',
                          multiValued = True,
                          isMetadata = True,
                          languageIndependent = False,
                          write_permission = ModifyPortalContent,
                          widget = ReferenceBrowserWidget(
                                                allow_search = True,
                                                allow_browse = True,
                                                show_indexes = False,
#                                                force_close_on_insert = True,
                                                label = _(u'label_related_items', default=u'Related Items'),
                                                description = '',
                                                visible = {'edit' : 'visible', 'view' : 'invisible' }
                                                )
                          ),
                                                                          
	atapi.ImageField('logo',
	                required=True,
#                    primary=True,
                    languageIndependent=True,
#                    storage = atapi.AnnotationStorage(migrate=True),
                    swallowResizeExceptions = zconf.swallowImageResizeExceptions.enable,
                    pil_quality = zconf.pil_config.quality,
                    pil_resize_algo = zconf.pil_config.resize_algo,
                    max_size = zconf.ATImage.max_image_dimension,
                    sizes= {
                            'mini'    : (200, 200),
                    },
                    validators = (('isNonEmptyFile', V_REQUIRED),
                                  ('checkImageMaxSize', V_REQUIRED)),
                    widget = atapi.ImageWidget(
					    label = _(u'label_info_sitelogo',default = u'Logo'),
						description = _(u'help_info_sitelogo',default=u'')
					)	               
	),
    atapi.StringField('tabs',
                      multiValued = True,
                      vocabulary = '_getTabs',
                      enforceVocabulary = 1,
                      widget = atapi.MultiSelectionWidget(
                                                          format = 'checkbox',
                                                          label = _(u'label_tabs', default=u'Tabs'),
                                                          description = ''
#                                                          visible = {'edit' : 'visible', 'view' : 'invisible' } 
                                                          )
                      ),

#    ReferenceDataGridField('order',
#                           schemata='default',
#                           relationship="order",
#                           widget = ReferenceDataGridWidget(
#                                                            label = "Order",
#                                                            visible = {'edit' : 'visible', 'view' : 'visible'}
#                                                            )
#                           ),
))


class ComixSite(ePubFolder):
    """A comix site"""
    portal_type      = 'ComixSite'
    archetype_name   = 'ComixSite'
    _atct_newTypeFor = {'portal_type' : 'CMF Folder', 'meta_type' : 'Comix Site'}

    implements(IComixSite)
    schema = ComixSiteSchema
    
    def _getTabs(self):
        return atapi.DisplayList((
            ('comix_view', _(u'type_info_comix',default=u'Comix')),
            ('hobby_view', _(u'type_info_hobby',default=u'Hobby')),
            ('image_view', _(u'type_info_image',default=u'Image')),
        ))
#    def searchList(self,type = "ComixFolder",maxSize=0):
#        results =[]
#        catalog = getToolByName(self,'portal_catalog')
#        path='/'.join(self.getPhysicalPath())
#        list = catalog.searchResults(path=path,portal_type = type)
#        for item in list:
#            results.append(item.getObject())
#        if maxSize==0:
#            return results 
#        else:
#            return results[:maxSize]
    
        
        
registerATCT(ComixSite,PROJECTNAME)